package com.mervin.patterns.singleton.type3;

/***************************************
 * @Auther: mervin
 * @Date: 2020-04-15 23:11
 * @Description: 懒汉式（线程不安全）
 ***************************************/
public class LazySingleton {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Singleton.getInstance());
            }).start();
        }
    }
}

class Singleton {

    private static Singleton instance;

    private Singleton(){}

    public static Singleton getInstance(){
        if(instance == null){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            instance = new Singleton();
        }
        return instance;
    }

}
